﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MO.ModelingClient
{
    public partial class ItemEnumControl : ItemBaseControl
    {
        object[] vaules;
        Dictionary<string, string> dict;
        public ItemEnumControl(string title, Dictionary<string, string> dict)
        {
            InitializeComponent();
            this.dict = dict.ToDictionary(k => k.Key, v => v.Value);
            vaules = dict.Keys.ToArray();
            comboBox.Items.AddRange(dict.Values.ToArray());
            base.Init(title);
        }
        public override void SetValue(object value)
        {
            if (value == null || !dict.ContainsKey(value.ToString()))
                return;
            comboBox.Text = dict[value.ToString()];
        }
        public override object GetValue()
        {
            if (comboBox.SelectedIndex < 0)
                return null;
            return vaules[comboBox.SelectedIndex];
        }
        public override void SetEnabled(bool enabled)
        {
            this.comboBox.Enabled = enabled;
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox.SelectedIndex < 0)
                return;
            if (base.ValueChanged != null)
                base.ValueChanged(GetValue());
        }
    }
}
